package com.liang.jiekou.config;


import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                // 通过.select()方法，配置扫描哪些API接口,RequestHandlerSelectors配置如何扫描接口
                .select()
                //any() // 扫描所有，项目中的所有接口都会被扫描到
                //none() // 不扫描接口
                // 通过方法上的注解扫描，如withMethodAnnotation(GetMapping.class)只扫描get请求
                //withMethodAnnotation(final Class<? extends Annotation> annotation)
                // 通过类上的注解扫描，如.withClassAnnotation(Controller.class)只扫描有controller注解的类中的接口
                //withClassAnnotation(final Class<? extends Annotation> annotation)
                //basePackage(final String basePackage) // 根据包路径扫描接口
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))  //添加ApiOperiation注解的被扫描
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        Contact contact = new Contact("1", "没有网址", "");
        return new ApiInfo(
                "接口文档",
                "接口文档",
                "版本1.9",
                "urn:tos",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}
//import io.swagger.annotations.ApiOperation;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
//import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//import springfox.documentation.builders.ApiInfoBuilder;
//import springfox.documentation.builders.PathSelectors;
//import springfox.documentation.builders.RequestHandlerSelectors;
//import springfox.documentation.service.ApiInfo;
//import springfox.documentation.spi.DocumentationType;
//import springfox.documentation.spring.web.plugins.Docket;
//import springfox.documentation.swagger2.annotations.EnableSwagger2;
//
//@Configuration
//  @EnableSwagger2
//  public class SwaggerConfig implements WebMvcConfigurer {
//
//              @Override
//      public void addResourceHandlers(ResourceHandlerRegistry registry) {
////                 registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
////                 registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
////                 registry.addResourceHandler("/swagger/**").addResourceLocations("classpath:/static/swagger/");
//                  registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
//
//                  /** 配置knife4j 显示文档 */
//                  registry.addResourceHandler("doc.html")
//                          .addResourceLocations("classpath:/META-INF/resources/");
//                  /**
//                   * 配置swagger-ui显示文档
//                   */
//                  registry.addResourceHandler("swagger-ui.html")
//                          .addResourceLocations("classpath:/META-INF/resources/");
//                  /** 公共部分内容 */
//                  registry.addResourceHandler("/webjars/**")
//                          .addResourceLocations("classpath:/META-INF/resources/webjars/");
//
//             }
//
//
//             @Bean
//     public Docket createRestApi() {
//                 return new Docket(DocumentationType.SWAGGER_2)
//                         .apiInfo(apiInfo())
//                         .select()
//                         //加了ApiOperation注解的类，才生成接口文档
//                         .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
//                         //包下的类，才生成接口文档
//                         .apis(RequestHandlerSelectors.basePackage("com.example.demologin.controller"))
//                         .paths(PathSelectors.any())
//                         .build()
//                         //在生产环境需要把swagger关闭，enable(false)
//                         .enable(true);
//             }
//
//             private ApiInfo apiInfo() {
//                 return new ApiInfoBuilder()
//                         .title("XXX公司")
//                         .description("XXXCRM-API文档")
//                         .termsOfServiceUrl("http://www.baidu.com/")
//                         .version("1.0")
//                         .build();
//             }
// }

